package com.dh.store;

import com.dh.util.TimeUtil;

import java.util.List;
import java.util.Scanner;

public class StoreTest {
    static Scanner scanner = new Scanner(System.in);
    static StoreManager sm = new StoreManager();

    public static void main(String[] args) {
        System.out.println("欢迎来到红浪漫商品系统");
        while (true) {
            printMenu();
            int num = scanner.nextInt();
            if (num == 1) {
                addGoods();
            } else if (num == 2) {
                deleteGoods();
            } else if (num == 3) {
                updateGoods();
            } else if (num == 4) {
                queryGoods();
            } else if (num == 5) {
                printGoods();
            } else if (num == 6) {
                break;
            }
        }
    }

    public static void printMenu() {
        System.out.println("1.增加商品");
        System.out.println("2.删除商品");
        System.out.println("3.更新商品");
        System.out.println("4.查找商品");
        System.out.println("5.打印商品");
        System.out.println("6.退出系统");
    }

    public static void addGoods() {
        System.out.println("请输入名字");
        String name = scanner.next();
        System.out.println("请输入价格");
        float price = scanner.nextFloat();
        Goods goods = new Goods(TimeUtil.createID(), name, price);
        sm.add(goods);
    }

    public static void deleteGoods() {
        System.out.println("请输入商品ID");
        String id = scanner.next();
        sm.delete(id);
    }

    public static void updateGoods() {
        System.out.println("请输入要更新商品ID");
        String id = scanner.next();
        Goods goods = sm.query(id);// 返回商品对象
        if (goods != null) {
            while (true) {
                System.out.println("1.更新名字");
                System.out.println("2.更新价格");
                System.out.println("3.退出更新");
                int num = scanner.nextInt();
                if (num == 1) {
                    System.out.println("请输入新的名字");
                    String name = scanner.next();
                    goods.setName(name);// 设置一个新的名字
                } else if (num == 2) {
                    System.out.println("请输入新的价格");
                    float price = scanner.nextFloat();
                    goods.setPrice(price);// 设置一个新的价格
                } else if (num == 3) {
                    break;
                }
                sm.update(goods);
            }
        } else {
            System.out.println("商品不存在");
        }


    }

    public static void queryGoods() {
        System.out.println("请输入要更新商品ID");
        String id = scanner.next();
        Goods goods = sm.query(id);// 返回商品对象
        if (goods != null) {
            System.out.println("商品ID:" + goods.getId());
            System.out.println("商品名字:" + goods.getName());
            System.out.println("商品价格:" + goods.getPrice());
        }
    }

    public static void printGoods() {
        List<Goods> gds = sm.getGds();// 返回所有商品集合
        for (Goods gd : gds) {
            System.out.println("商品ID:" + gd.getId());
            System.out.println("商品名字:" + gd.getName());
            System.out.println("商品价格:" + gd.getPrice());
            System.out.println("------------------------");
        }
    }
}
